package com.binhai.user;

import com.alibaba.fastjson.JSON;
import com.binhai.db.SpringDB;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class UserDao {
    @Autowired
    private SpringDB springDB;
    public List getAllInfos() {
        try {
            JdbcTemplate jdbcTemplate = springDB.getJdbcTemplate();
            String sql = "select * from t_user where  state=0";
            //使用模版查询，返回list列表
            List list = jdbcTemplate.queryForList(sql);
            //响应数据
            return list;

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    public Map login(UserBean bean){
        Map map = new HashMap();
        try {
            JdbcTemplate jdbcTemplate = springDB.getJdbcTemplate();
            String sql = "select * from t_user where name='"+bean.getName()+"' and pass='"+bean.getPass()+"'";
            List list = jdbcTemplate.queryForList(sql);

            if(list.size()>0){
                map.put("result","success");
            }else{
                map.put("result","error");
            }
        } catch (Exception e){
            e.printStackTrace();
            map.put("result","error");
        }
        return map;
    }

    public Map addInfo(UserBean bean) {
        Map map = new HashMap();
        try {
            JdbcTemplate jdbcTemplate = springDB.getJdbcTemplate();
            String sql = "insert into t_user(name,pass,tel,beizhu) values('"+bean.getName()+"','"+bean.getPass()+"','"+bean.getTel()+"','"+bean.getBeizhu()+"')";
            int i = jdbcTemplate.update(sql);
            if(i>0){
                map.put("result","success");
            }else{
                map.put("result","error");
            }
        } catch (Exception e){
            e.printStackTrace();
            map.put("result","error");
        }
        return map;
    }

    public Map deleteInfo(int id) {
        Map map = new HashMap();
        try {
            JdbcTemplate jdbcTemplate = springDB.getJdbcTemplate();
            String sql = "update t_user set state=1 where id="+id;
            int i = jdbcTemplate.update(sql);
            if(i>0){
                map.put("result","success");
            }else{
                map.put("result","error");
            }
        } catch (Exception e){
            e.printStackTrace();
            map.put("result","error");
        }
        return map;
    }
    public Map modifyInfo(UserBean bean) {
        Map map = new HashMap();
        try {
            JdbcTemplate jdbcTemplate = springDB.getJdbcTemplate();
            String sql = "update t_user set name='"+bean.getName()+"',pass='"+bean.getPass()+"',tel='"+bean.getTel()+"',beizhu='"+bean.getBeizhu()+"' where id="+bean.getId();
            int i = jdbcTemplate.update(sql);
            if(i>0){
                map.put("result","success");
            }else{
                map.put("result","error");
            }
        } catch (Exception e){
            e.printStackTrace();
            map.put("result","error");
        }
        return map;
    }

    public List selectByWhere(String name) {
        try {
            JdbcTemplate jdbcTemplate = springDB.getJdbcTemplate();
            String sql = "select * from t_user where name like '%"+name+"%' and state=0";
            List list = jdbcTemplate.queryForList(sql);
            return list;
        } catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }
}

